package it.valeandre.omino.adapter;

import it.valeandre.omino.R;
import it.valeandre.omino.utils.TypedPair;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Getta on 02/02/14.
 */
public class TimelineAdapter extends BaseAdapter {

    private List<TypedPair<String, String>> mEvents = new ArrayList<TypedPair<String, String>>();
    private Handler mHandler = new Handler();
    private LayoutInflater mInflater;
    private Context mContext;


    public TimelineAdapter(Context context, List<TypedPair<String, String>> events) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mEvents = events;
    }

    public int getCount() {
        return mEvents.size();
    }

    public TypedPair<String, String> getItem(int pos) {
        return mEvents.get(pos);
    }

    public long getItemId(int pos) {
        return pos;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    public View getView(int pos, View view, ViewGroup viewGroup) {

        TypedPair<String, String> item = getItem(pos);

        //if (view == null) {
            switch (item.getMatchType()) {
                case AWAY:
                    view = mInflater.inflate(R.layout.timeline_adapter_right_item, null);
                    break;
                case HOME:
                    view = mInflater.inflate(R.layout.timeline_adapter_left_item, null);
                    break;
                default:
                    view = null;
                    break;
            }

        //}

        ImageView image = (ImageView) view.findViewById(R.id.timeline_adapter_item_event_image);
        switch (item.getEventType()) {
            case YELLOW:
                image.setImageResource(R.drawable.yellow_card);
                break;
            case RED:
                image.setImageResource(R.drawable.red_card);
                break;
            case GOAL:
                image.setImageResource(R.drawable.palla_calcio);
        }

        TextView minute = (TextView) view.findViewById(R.id.timeline_adapter_item_minute);
        minute.setText(item.getMinute());

        TextView author = (TextView) view.findViewById(R.id.timeline_adapter_item_author);
        author.setText(item.getAuthor().replace("penalty", mContext.getResources().getString(R.string.penalty)));

        return view;
    }
}
